import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xin_nuo_pda/bean/lot_entity.dart';
import 'package:xin_nuo_pda/product/add_material/AddMaterialController.dart';
import 'package:xin_nuo_pda/utils/string_extension.dart';
import 'package:xin_nuo_pda/values/colors.dart';
import 'package:xin_nuo_pda/values/constant.dart';
import 'package:xin_nuo_pda/views/SubTitleBar.dart';
import 'package:xin_nuo_pda/views/TextView.dart';
import 'package:xin_nuo_pda/views/edittext/TitleEditText.dart';

class AddMaterialPage extends StatelessWidget {
  final controller = Get.put(AddMaterialController());
  final state = Get.find<AddMaterialController>().state;
  TextEditingController textController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(USER)),
      body: SingleChildScrollView(
        child: GetBuilder<AddMaterialController>(
          builder: (controller) => Container(
            child: Column(
              children: [
                SubTitleBar("增加物料", "提交", () {
                  controller.addMaterial();
                }),
                TitleEditText(
                  "批次ID",
                  "请扫描批次ID",
                  (value) {
                    state.lotID = value;
                    controller.getLotInfo();
                  },
                  controller: textController,
                ),
                GetBuilder<AddMaterialController>(
                    builder: (controller) => Container(
                        child: Column(
                            children: <Widget>[
                              SizedBox(
                                height: 1,
                              ),
                              TextView("批次数量",  state.lotEntity?.mAINQTY.toString() ?? '0'),
                              SizedBox(
                                height: 1,
                              ),
                              TextView("批次状态", state.lotEntity?.sTATE.toString() ?? ''),
                              SizedBox(
                                height: 1,
                              ),
                              TextView("当前工步", (state.lotEntity?.sTEPNAME.toString() ?? '') + '/' + (state.lotEntity?.sTEPDESC.toString() ?? '')),
                              SizedBox(
                                height: 1,
                              ),
                              TextView("设备ID", state.lotEntity?.eQUIPMENTID.toString() ?? '0'),
                              SizedBox(
                                height: 1,
                              ),
                            ],
                        ),
                    ),
                  id: state.lotEntityId,
                ),
                Container(
                  height: 48,
                  padding: const EdgeInsets.all(8),
                  decoration: BoxDecoration(
                    color: Colors.white,
                  ),
                  width: double.infinity,
                  child: Stack(
                    children: [
                      Align(
                        alignment: Alignment.centerLeft,
                        child: Text(
                          "批次Bom信息",
                          style: TextStyle(
                              fontSize: 16, fontWeight: FontWeight.bold),
                        ),
                      ),
                      Align(
                        alignment: Alignment.centerRight,
                        child: Container(
                          height: 30,
                          margin: EdgeInsets.only(right: 8),
                          child: OutlinedButton(
                            onPressed: () {
                              controller.getCheckList();
                            },
                            child: Text("添加"),
                          ),
                        ),
                      )
                    ],
                  ),
                ),
                GetBuilder<AddMaterialController>(
                  builder: (controller) => Table(
                    defaultVerticalAlignment: TableCellVerticalAlignment.middle,
                    columnWidths: {
                      //列宽
                      0: FlexColumnWidth(1.0),
                      1: FlexColumnWidth(1.0),
                      2: FlexColumnWidth(1.0),
                      3: FlexColumnWidth(1.0),
                      4: FlexColumnWidth(1.0),
                      5: FlexColumnWidth(1.0)
                    },
                    border: TableBorder.all(
                      color: Colors.grey,
                      width: 1.0,
                      style: BorderStyle.solid,
                    ),
                    children: getBomList(context),
                  ),
                  id: state.tableControllerId,
                ),
                SizedBox(
                  height: 1,
                ),
                Container(
                  margin: EdgeInsets.all(8),
                  alignment: Alignment.centerLeft,
                  child: Text(
                    "添加物料信息",
                    style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
                  ),
                ),
                GetBuilder<AddMaterialController>(
                  builder: (controller) => Table(
                    defaultVerticalAlignment: TableCellVerticalAlignment.middle,
                    columnWidths: {
                      //列宽
                      0: FlexColumnWidth(1.0),
                      1: FlexColumnWidth(1.0),
                      2: FlexColumnWidth(1.0),
                      3: FlexColumnWidth(1.0),
                      4: FlexColumnWidth(1.0),
                      5: FlexColumnWidth(1.0)
                    },
                    border: TableBorder.all(
                      color: Colors.grey,
                      width: 1.0,
                      style: BorderStyle.solid,
                    ),
                    children: getMaterialList(context),
                  ),
                  id: state.addControllerId,
                )
              ],
            ),
          ),
        ),
      ),
    );
  }

  List<TableRow> getBomList(BuildContext context) {
    List<TableRow> tempView = [];
    List<FocusNode> focusNodeList = [];
    tempView.add(TableRow(
        children: [
          //增加行高
          SizedBox(
            height: 40,
            child: Align(
              alignment: Alignment.center,
              child: Text(
                '选择',
                style: TextStyle(
                  color: theme_blue.toColor(),
                ),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '物料料号',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '物料类型',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '单位',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '单位用量',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          )
        ],
        //第一行样式 添加背景色
        decoration: BoxDecoration(
          color: table_head.toColor(),
        )));
    for (int i = 0; i < state.bomList.length; i++) {
      LotBOMLINELIST element = state.bomList[i];
      focusNodeList.add(FocusNode());
      tempView.add(TableRow(children: [
        //增加行高
        SizedBox(
          height: 40,
          child: Align(
            alignment: Alignment.center,
            child: Checkbox(
                onChanged: (value) {
                  // controller.updateGroupValue(value);
                  element.isSelector = value!;
                  controller.update([state.tableControllerId]);
                },
                value: element.isSelector),
          ),
        ),
        Align(
          alignment: Alignment.center,
          child: Text(
            element.mATERIALNAME ?? '',
            style: TextStyle(
              color: text_black.toColor(),
            ),
          ),
        ),
        Align(
          alignment: Alignment.center,
          child: Text(
            element.mATERIALTYPE ?? '',
            style: TextStyle(
              color: text_black.toColor(),
            ),
          ),
        ),
        Align(
          alignment: Alignment.center,
          child: Text(
            element.uOMID ?? '',
            style: TextStyle(
              color: text_black.toColor(),
            ),
          ),
        ),
        Align(
          alignment: Alignment.center,
          child: Text(
            element.uNITQTY ?? '',
            style: TextStyle(
              color: text_black.toColor(),
            ),
          ),
        )
      ]));
    }
    return tempView;
  }

  getMaterialList(BuildContext context) {
    List<TableRow> tempView = [];
    List<FocusNode> focusNodeList = [];
    tempView.add(TableRow(
        children: [
          //增加行高
          SizedBox(
            height: 40,
            child: Align(
              alignment: Alignment.center,
              child: Text(
                '序号',
                style: TextStyle(
                  color: theme_blue.toColor(),
                ),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '物料料号',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '物料类型',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '物料批次',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '加工生产\n批次数量',
              style: TextStyle(
                color: theme_blue.toColor(),
                fontSize: 14,
              ),
            ),
          )
        ],
        //第一行样式 添加背景色
        decoration: BoxDecoration(
          color: table_head.toColor(),
        )));
    for (int i = 0; i < state.materialList.length; i++) {
      LotBOMLINELIST element = state.materialList[i];
      focusNodeList.add(FocusNode());
      tempView.add(TableRow(children: [
        //增加行高
        SizedBox(
          height: 40,
          child: Align(
            alignment: Alignment.center,
            child: Text(
              (i + 1).toString(),
              style: TextStyle(color: text_black.toColor()),
            ),
          ),
        ),
        Align(
          alignment: Alignment.center,
          child: Text(
            element.mATERIALNAME ?? '',
            style: TextStyle(
              color: text_black.toColor(),
            ),
          ),
        ),
        Align(
          alignment: Alignment.center,
          child: Text(
            element.mATERIALTYPE ?? '',
            style: TextStyle(
              color: text_black.toColor(),
            ),
          ),
        ),
        Align(
          alignment: Alignment.center,
          child: Text(
            element.MLOTID ?? '',
            style: TextStyle(
              color: text_black.toColor(),
            ),
          ),
        ),
        Align(
          alignment: Alignment.center,
          child: Text(
            element.PROCESSLOTQTY ?? '',
            style: TextStyle(
              color: text_black.toColor(),
            ),
          ),
        )
      ]));
    }
    return tempView;
  }
}
